package com.robusta.cityuate.utils;

import com.robusta.cityuate.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

public class ConnectionUtils {

	private static final String SHARED_PREFS_CONNECTION = "CITYUATE_SHARED";

	@SuppressWarnings("unused")
	private static final String TAG = "Util";

	/**
	 * Used to save variable indicating existing of connection
	 * 
	 * @param context
	 * @param key
	 *            a unique key to save the variable and fetch it using this key
	 * @author mhm
	 */
	public static void setConnecting(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(key + "Con");
		editor.putBoolean(key + "Con", true);
		editor.commit();
	}

	/**
	 * sets the connecting variable to false and may be any extra info
	 * 
	 * @param context
	 * @param key
	 *            a unique key to save the variable and fetch it using this key
	 * @param extra
	 *            any extra information about the finish of connection is it
	 *            smooth or not if success or failure in order to act
	 *            according to the message
	 * @author mhm
	 */
	public static void setDisconnecting(Context context,boolean result, String key,
			String extra) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(key + "Con");
		editor.putBoolean(key + "OldCon",true);
		editor.putBoolean(key + "Con_result", result);
		if (extra != null) {
			editor.remove(key + "Con_extra");
			editor.putString(key + "Con_extra", extra);
		}
		editor.commit();
	}

	/**
	 * checks if this key connection is active or not.
	 * 
	 * @param context
	 * @param key
	 *            a unique key per variable to save the variable and fetch it
	 *            using this key
	 * @return returns true if the connection is on and false if there is no
	 *         connection active
	 * @author mhm
	 */
	public static boolean isConnecting(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(key + "Con", false);
	}
	
	/**
	 * check if there was aconnection and it's disconnected
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean wasConnected(Context context, String key){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(key + "OldCon", false);
	}
	
	
	/**
	 * returns if the connection result was successful or not
	 * @param context
	 * @param key
	 * @return
	 */
	
	public static boolean isResultSuccessful(Context context, String key){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(key + "Con_result", false);
	}
	

	/**
	 * to get the extra saved in case of disconnecting may be indicating the
	 * reason behind disconnecting if success or failure.
	 * 
	 * @param context
	 * @param key
	 *            a unique key per variable to save the variable and fetch it
	 *            using this key
	 * @return null in case of no info saved and String other wise
	 */
	public static String getExtra(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		return sharedPreferences.getString(key + "Con_extra", null);

	}

	public static void clearConnection(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(key + "Con");
		editor.remove(key + "Con_extra");
		editor.remove(key+"OldCon");
		editor.remove(key+"Con_result");
		
		editor.commit();
	}
	
	
	/**
	 * Checks if the network providers are available or not
	 * 
	 * @param mContext
	 * @return
	 */

	public static boolean isNetworkAvailable(Context mContext) {
		ConnectivityManager connectivityManager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		return activeNetworkInfo != null;
	}

	/**
	 * Display a dialog that user has no internet connection
	 * 
	 * @param ctx1
	 * 
	 */
	public static void showNoConnectionDialog(Context ctx1) {
		final Context ctx = ctx1;
		AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
		builder.setCancelable(true);
		builder.setMessage(R.string.no_connection);
		builder.setTitle(R.string.no_connection_title);
		builder.setPositiveButton(R.string.settings,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						ctx.startActivity(new Intent(
								Settings.ACTION_WIRELESS_SETTINGS));
					}
				});
		builder.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});
		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
			public void onCancel(DialogInterface dialog) {
				return;
			}
		});

		builder.show();
	}
	
	
	
	

}
